package com.hf.lookatme.utils;

import android.content.Context;
import android.graphics.Bitmap;

import com.hf.lookatme.R;
import com.hf.lookatme.commons.DataConst;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.orhanobut.logger.Logger;

import java.io.File;

/**
 * Created by JoeShiguang on 2015/5/20.
 */
public class ImageLoaderConfigUtils {

  private static String TAG = "ImageLoaderConfigUtils";

  public static ImageLoader imageLoader;

  public static ImageLoader instanceImageLoader(Context context) {

    if (imageLoader == null) {
      imageLoader = ImageLoader.getInstance();
      initImageLoaderConfig(context);
    }
    return imageLoader;
  }


  private static void initImageLoaderConfig(Context context) {

    ImageLoaderConfiguration.Builder builder_configuration = new ImageLoaderConfiguration.Builder(context);
    builder_configuration.memoryCacheExtraOptions(800, 800);
    builder_configuration.threadPoolSize(5); // 线程池数
    builder_configuration.diskCache(new UnlimitedDiscCache(new File(SDCardUtils.getSDCartPath() + DataConst.PATH_CACHE)));
    builder_configuration.diskCacheSize(50 * 1920 * 1080);
    builder_configuration.diskCacheFileCount(100);
    builder_configuration.tasksProcessingOrder(QueueProcessingType.LIFO);
    builder_configuration.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000));

    ImageLoaderConfiguration configuration = builder_configuration.build();
    imageLoader.init(configuration);
  }

  public static DisplayImageOptions createDisplayHeadImageOptions() {
    DisplayImageOptions options = null;
    try {
      options = new DisplayImageOptions.Builder()
          .showImageForEmptyUri(R.drawable.img_mr_round) // 没有图片资源时的默认图片
          .showImageOnFail(R.drawable.img_mr_round) // 加载失败时的图片*/
          .cacheInMemory(true) // 启用内存缓存*/
          .cacheOnDisk(true) // 启用外存缓存
          .bitmapConfig(Bitmap.Config.RGB_565)
          .displayer(new RoundedBitmapDisplayer(200)) // 设置显示风格这里是圆角矩形
                    /*.displayer(new FadeInBitmapDisplayer(3000))   这里的displayer只能使用一种*/
          .build();
    } catch (Exception ex) {
      Logger.e(TAG, "createDisplayHeadImageOptions failed.");
    }
    return options;
  }


  /**
   *
   String imageUri = "http://site.com/image.png"; // from Web
   String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
   String imageUri = "content://media/external/audio/albumart/13"; // from content provider
   String imageUri = "assets://image.png"; // from assets
   String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
   *
   */
}
